package com.hello;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.ianywhere.ultralitejni12.ConfigPersistent;
import com.ianywhere.ultralitejni12.Connection;
import com.ianywhere.ultralitejni12.DatabaseManager;
import com.ianywhere.ultralitejni12.PreparedStatement;
import com.ianywhere.ultralitejni12.ResultSet;
import com.ianywhere.ultralitejni12.StreamHTTPParms;
import com.ianywhere.ultralitejni12.SyncParms;
import com.ianywhere.ultralitejni12.ULjException;

public class AndroidDB {
	
	public String checkstatement = "Select * from MetaData";
	String hostportstmt = "SELECT host, port FROM MetaData", host = "192.168.1.100";
	int port = 50000;
	Context context = null;
    ConfigPersistent config;
	private Connection conn;
	
	public AndroidDB(Context applicationContext) {
		context = applicationContext;
	}

	public Connection initialize() throws ULjException {
		String dbname = "s2_remote.udb";
		
		//conifg moze istniec bo jest persistent
		if(config == null){
			config = createConfig(dbname);
			config.setLazyLoadIndexes(true);
		}
		
		try {
			conn = DatabaseManager.connect(config);
		} catch (ULjException e){
			conn = DatabaseManager.createDatabase(config);
		}
		
		//sprawdzam czy istnieja tabele
		String exist = null;
		try {
			exist = executeSingleValueQuery(checkstatement);
		} catch (ULjException e){
			// ...
		}
		
		//tworzenie tabel jezeli to nowa baza
		if (exist == null){
			SchemaCreation schema = new SchemaCreation(conn); 
			schema.create();
			//schema.executeStmt("insert into MetaData (host, port) values ('" + host + "'," + port + ")");
		}
		
		//pobieramy hosta i port
		selectHostPort();
		return conn;
	}

	private void selectHostPort() throws ULjException {
		PreparedStatement ps = conn.prepareStatement(hostportstmt);
		try{
            ResultSet rs = ps.executeQuery();
            if( rs.next() ) {
                host = rs.getString( 1 );
                port = rs.getInt( 2 );
            }
		} finally {
			ps.close();
		}
		
	}

	private ConfigPersistent createConfig(String dbname) throws ULjException {
		return DatabaseManager.createConfigurationFileAndroid(dbname, context);
	}
	
    public String executeSingleValueQuery( String query )
            throws ULjException
        {
            String retn = null;
            PreparedStatement ps = conn.prepareStatement( query );
            try {
                ResultSet rs = ps.executeQuery();
                if( rs.next() ) {
                    retn = rs.getString( 1 );
                }
            } finally {
                ps.close();
            }
            return retn;
        }

   public void synchronize() throws ULjException{
	   SyncParms sp = conn.createSyncParms("remote", "s2_scriptversion");
	   StreamHTTPParms streamparms = sp.getStreamParms();
	   streamparms.setHost(host);
	   streamparms.setPort(port);
	   
	   conn.synchronize(sp);
	   
   }
	
	public void insertHostPort(String host1, int port1) throws ULjException {
		host = host1;
		port = port1;
		
		String stmt = "insert into metadata (host, port) values ('" + host + "', " + port + ")";
		(new SchemaCreation(conn)).executeStmt(stmt); //uwaga TODO poprawic
	}	
   
}
